package com.zeekr.sdk.storage;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.zeekr.sdk.storage.core.StorageHandlerManager;
import com.zeekr.sdk.storage.databinding.ActivityMainBinding;


public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private static final String testStringKey = "testStringKey";
    private static final String testIntKey = "testIntKey";
    private static final String testBooleanKey = "testBooleanKey";
    private static final String testLongKey = "testLongKey";
    private static final String testFloatKey = "testFloatKey";
    private static final String testDoubleKey = "testDoubleKey";
    private static final String jsonStr = "{\"StringValue\":\"mwbp\",\"intValue\":1,\"longValue\":1,\"floatValue\":1.0,\"doubleValue\":0.0999999,\"remoteUrl\":null,\"booleanValue\":true}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.tvOrigin.setText(jsonStr);
        binding.btnSaveMmkv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StorageHandlerManager.getCache("mmkv_storage").putValue(testStringKey, jsonStr);
                StorageHandlerManager.getCache("mmkv_storage").putValue(testIntKey, 5);
                StorageHandlerManager.getCache("mmkv_storage").putValue(testBooleanKey, true);
                StorageHandlerManager.getCache("mmkv_storage").putValue(testLongKey, 1234L);
                StorageHandlerManager.getCache("mmkv_storage").putValue(testFloatKey, 6.9F);
                StorageHandlerManager.getCache("mmkv_storage").putValue(testDoubleKey, 0.06+0.01);
            }
        });
        binding.btnSaveSp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StorageHandlerManager.getCache("sp_storage").putValue(testStringKey, jsonStr);

            }
        });
        binding.btnSaveFile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StorageHandlerManager.getCache("file_storage").putValue(testStringKey, jsonStr);
            }
        });

        binding.btnGetResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initResultValue();
            }
        });
    }

    @Override
    protected void onDestroy() {
        StorageHandlerManager.destroyCache("mmkv_storage");
        StorageHandlerManager.destroyCache("sp_storage");
        StorageHandlerManager.destroyCache("file_storage");
        super.onDestroy();
    }

    private void initResultValue() {
        Log.e("MainActivity", "get testString:"+getTestString());
        Log.e("MainActivity", "get testInt:"+getTestInt());
        Log.e("MainActivity", "get testBoolean:"+getTestBoolean());
        Log.e("MainActivity", "get testLong:"+getTestLong());
        Log.e("MainActivity", "get testFloat:"+getTestFloat());
        Log.e("MainActivity", "get testDouble:"+getTestDouble());
        binding.tvMmkvResult.setText(StorageHandlerManager.getCache("mmkv_storage").getValue(testStringKey, ""));
        binding.tvSpResult.setText(StorageHandlerManager.getCache("sp_storage").getValue(testStringKey, ""));
        binding.tvFileResult.setText(StorageHandlerManager.getCache("file_storage").getValue(testStringKey, ""));
    }

    private String getTestString(){
        return StorageHandlerManager.getCache("mmkv_storage").getValue(testStringKey, "");
    }


    private int getTestInt(){
        return StorageHandlerManager.getCache("mmkv_storage").getValue(testIntKey, 0);
    }

    private boolean getTestBoolean(){
        return StorageHandlerManager.getCache("mmkv_storage").getValue(testBooleanKey, false);
    }

    private long getTestLong(){
        return StorageHandlerManager.getCache("mmkv_storage").getValue(testLongKey, 0L);
    }

    private float getTestFloat(){
        return StorageHandlerManager.getCache("mmkv_storage").getValue(testFloatKey, 0.0F);
    }

    private double getTestDouble(){
        return StorageHandlerManager.getCache("mmkv_storage").getValue(testDoubleKey, 0.00);
    }
}